package com.ldk.admin.controller;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.admin.common.ResultVo;
import com.ldk.admin.constants.CONFIG;
import com.ldk.admin.constants.SESSION;
import com.ldk.admin.constants.ViewMode;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.DateUtil;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.AdminUser;
import com.ldk.framework.entity.Companypermit;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.ServiceConfig;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.entity.ServiceTripCrews;
import com.ldk.framework.entity.TableBean;
import com.ldk.framework.entity.TripStatusRecord;
import com.ldk.framework.entity.User;
import com.ldk.framework.enums.PlatformUserType;
import com.ldk.framework.enums.TripStatus;
import com.ldk.framework.service.ICompanypermitService;
import com.ldk.framework.service.IDriverService;
import com.ldk.framework.service.IServiceConfigService;
import com.ldk.framework.service.IServiceTripCrewsService;
import com.ldk.framework.service.IServiceTripService;
import com.ldk.framework.service.ITripStatusRecordService;
import com.ldk.framework.service.IUserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 行程列表
 *
 * @author xiangwl
 * 2017年3月30日
 * com.ldk.admin.controller
 * TripController
 */
@Controller
@RequestMapping("/trip")
public class TripController extends BaseController {

    /**
     * 路由地址
     */
    private final String route = "trip";
    /**
     * 模块名称
     */
    private final String module = "行程订单";

    @Autowired
    private IServiceTripService serviceTripService;
    @Autowired
    private IServiceConfigService serviceConfigService;
    @Autowired
    private IServiceTripCrewsService serviceTripCrewsService;
    @Autowired
    private ITripStatusRecordService tripStatusRecordService;
    @Autowired
    private ICompanypermitService companypermitService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDriverService driverService;

    @RequestMapping("/list")
    //public ModelAndView list(ModelMap map){
    public ModelAndView list(ModelMap map, HttpServletRequest request,
                             HttpServletResponse response) {
        //session是否过期
        this.isExpired(request, response);
        setLinks(map, "", "", module + "管理", "/" + route + "/list", "", "");
        map.addAttribute("route", route);
        map.addAttribute("module", module);
        map.addAttribute("serviceCodeList", getServiceConfigList());
        map.addAttribute("companyList", getCompanyList());
        //操作日志
        //this.insertOplog(request,module+"管理","","","查看",this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());
        String areaName = this.getAdminAreaName(request);
        map.addAttribute("areaName", areaName);
        return new ModelAndView("/trip/list");
    }

    @RequestMapping("/list1")
    //public ModelAndView list(ModelMap map){
    public ModelAndView list1(ModelMap map, HttpServletRequest request,
                              HttpServletResponse response) {
        //session是否过期
        this.isExpired(request, response);
        setLinks(map, "", "", module + "管理", "/" + route + "/list", "", "");
        map.addAttribute("route", route);
        map.addAttribute("module", module);
        map.addAttribute("serviceCodeList", getServiceConfigList());
        map.addAttribute("companyList", getCompanyList());
        //操作日志
        //this.insertOplog(request,module+"管理","","","查看",this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());
        String areaName = this.getAdminAreaName(request);
        map.addAttribute("areaName", areaName);
        return new ModelAndView("/trip/list1");
    }

    @ResponseBody
    @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public LigerUIGridResult<ServiceTrip> getList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                  @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pageSize,
                                                  @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                                  @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime,
                                                  @RequestParam(value = "status", required = false, defaultValue = "") Integer status,
                                                  @RequestParam(value = "serviceCode", required = false, defaultValue = "") String serviceCode,
                                                  @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                                                  @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey,
                                                  HttpServletRequest request) {
        int limit = pageSize;
        int offset = (page - 1) * pageSize;

        ServiceTrip query = new ServiceTrip();
        query.setStatus(status);
        query.setServiceCode(serviceCode);
        if (!StringUtils.isEmpty(searchKey)) {
            query.setSearchKey(searchKey);
        }
        if (!StringUtils.isEmpty(companyId)) {
            query.setCompanyId(companyId);
        }
        if (!StringUtils.isEmpty(startTime)) {
            long st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            query.setStartTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            long et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            query.setEndTime(et);
        }

        String areaName = this.getAdminAreaName(request);

        //System.out.println("trip/getList areaName:"+areaName);

        if (areaName != null && areaName != "") {
            query.setSearchAreaKey(areaName);
        }
        AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            query.setCompanyId(admin.getCompanyId());
        }
        query.setUserType(1);

        Sort sort = new Sort(Direction.DESC, "create_time");
        CustomPageable pageable = new CustomPageable(offset, limit, sort);
        //Page<ServiceTrip> result = serviceTripService.selectPageList("", query, pageable);
        Page<ServiceTrip> result = serviceTripService.selectPageList("", query, pageable);

        LigerUIGridResult<ServiceTrip> grid = new LigerUIGridResult<>();
        grid.Total = result.getTotalElements();
        grid.Rows = result.getContent();

        return grid;
    }

    @ResponseBody
    @RequestMapping(value = "/getList1", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public LigerUIGridResult<ServiceTrip> getList1(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                   @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pageSize,
                                                   @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                                   @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime,
                                                   @RequestParam(value = "status", required = false, defaultValue = "") Integer status,
                                                   @RequestParam(value = "serviceCode", required = false, defaultValue = "") String serviceCode,
                                                   @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                                                   @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey,
                                                   HttpServletRequest request) {
        int limit = pageSize;
        int offset = (page - 1) * pageSize;

        ServiceTrip query = new ServiceTrip();
        query.setStatus(status);
        query.setServiceCode(serviceCode);
        if (!StringUtils.isEmpty(searchKey)) {
            query.setSearchKey(searchKey);
        }
        if (!StringUtils.isEmpty(companyId)) {
            query.setCompanyId(companyId);
        }
        if (!StringUtils.isEmpty(startTime)) {
            long st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            query.setStartTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            long et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            query.setEndTime(et);
        }

        String areaName = this.getAdminAreaName(request);

        //System.out.println("trip/getList areaName:"+areaName);

        if (areaName != null && areaName != "") {
            query.setSearchAreaKey(areaName);
        }
        AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            query.setCompanyId(admin.getCompanyId());
        }
        query.setUserType(2);
        Sort sort = new Sort(Direction.DESC, "create_time");
        CustomPageable pageable = new CustomPageable(offset, limit, sort);
        //Page<ServiceTrip> result = serviceTripService.selectPageList("", query, pageable);
        Page<ServiceTrip> result = serviceTripService.selectPageList("", query, pageable);

        LigerUIGridResult<ServiceTrip> grid = new LigerUIGridResult<>();
        grid.Total = result.getTotalElements();
        grid.Rows = result.getContent();

        return grid;
    }

    /**
     * 浏览
     *
     * @param tripNo
     * @param map
     * @return
     */
    @RequestMapping("/view/{tripNo}")
    //public ModelAndView view(@PathVariable(value="tripNo") String tripNo, ModelMap map){
    public ModelAndView view(@PathVariable(value = "tripNo") String tripNo, ModelMap map, HttpServletRequest request,
                             HttpServletResponse response) {
        //session是否过期
        this.isExpired(request, response);
        setViewMode(map, ViewMode.View);
        ServiceTrip query = new ServiceTrip();
        query.setTripNo(tripNo);
        ServiceTrip model = serviceTripService.selectByKey("", query);

        map.addAttribute("module", module);

        //操作日志
        this.insertOplog(request, module, "", "", "查看:" + tripNo, this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        return new ModelAndView("/trip/view", "model", model);
    }

    @RequestMapping("/crews/{tripNo}")
    //public ModelAndView crews(@PathVariable(value="tripNo") String tripNo, ModelMap map){
    public ModelAndView crews(@PathVariable(value = "tripNo") String tripNo, ModelMap map, HttpServletRequest request,
                              HttpServletResponse response) {
        //session是否过期
        this.isExpired(request, response);
        setLinks(map, module + "管理", "/" + route + "/list", "乘客列表", "/" + route + "/crews/" + tripNo, "", "");
        map.addAttribute("route", route);
        map.addAttribute("module", "行程乘客");
        map.addAttribute("tripNo", tripNo);

        //操作日志
        this.insertOplog(request, module, "", "", "行程乘客:" + tripNo, this.getAdminUserName(request).toString(),
                Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        return new ModelAndView("/trip/crews/list");
    }

    @ResponseBody
    @RequestMapping(value = "/crews/getList", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public LigerUIGridResult<ServiceTripCrews> getTripCrewsList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                                @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pageSize,
                                                                @RequestParam(value = "tripNo", required = false, defaultValue = "") String tripNo) {
        ServiceTrip tripQuery = new ServiceTrip();
        tripQuery.setTripNo(tripNo);
        ServiceTrip trip = serviceTripService.selectByKey("", tripQuery);
        if (trip == null) return null;
        String driverTripNo = "";
        if (trip.getUserType() == PlatformUserType.CLIENT_USER.getValue()) {

            ServiceTripCrews query = new ServiceTripCrews();
            query.setCrewTripNo(tripNo);
            query.setCrewOpenId(trip.getOpenId());

            ServiceTripCrews tripCrews = serviceTripCrewsService.selectOne("", query);
            driverTripNo = tripCrews == null ? "" : tripCrews.getDriverTripNo();

        } else {
            driverTripNo = tripNo;
        }
        if (StringUtils.isBlank(driverTripNo)) return null;


        int limit = pageSize;
        int offset = (page - 1) * pageSize;

        CustomPageable pageable = new CustomPageable(offset, limit);
        ServiceTripCrews query = new ServiceTripCrews();
        query.setDriverTripNo(driverTripNo);

        Page<ServiceTripCrews> result = serviceTripCrewsService.selectPageList("", query, pageable);

        LigerUIGridResult<ServiceTripCrews> grid = new LigerUIGridResult<>();
        grid.Total = result.getTotalElements();
        grid.Rows = result.getContent();

        return grid;

    }

    @RequestMapping("/status/{tripNo}")
    //public ModelAndView status(@PathVariable(value="tripNo") String tripNo, ModelMap map){
    public ModelAndView status(@PathVariable(value = "tripNo") String tripNo, ModelMap map, HttpServletRequest request,
                               HttpServletResponse response) {
        //session是否过期
        this.isExpired(request, response);
        setLinks(map, module + "管理", "/" + route + "/list", "状态列表", "/" + route + "/crews/" + tripNo, "", "");
        map.addAttribute("route", route);
        map.addAttribute("module", "行程状态");
        map.addAttribute("tripNo", tripNo);

        //操作日志
        this.insertOplog(request, module, "", "", "行程状态:" + tripNo, this.getAdminUserName(request).toString(),
                Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        return new ModelAndView("/trip/status/list");
    }

    @ResponseBody
    @RequestMapping(value = "/status/getList", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public LigerUIGridResult<TripStatusRecord> getStatusList(@RequestParam(value = "tripNo", required = false, defaultValue = "") String tripNo) {
        ServiceTrip tripQuery = new ServiceTrip();
        tripQuery.setTripNo(tripNo);
        ServiceTrip trip = serviceTripService.selectByKey("", tripQuery);
        if (trip == null) return null;


        TripStatusRecord query = new TripStatusRecord();
        query.setOrderNo(tripNo);

        List<TripStatusRecord> result = tripStatusRecordService.selectList("", query);

        LigerUIGridResult<TripStatusRecord> grid = new LigerUIGridResult<>();
        grid.Total = result.size();
        grid.Rows = result;

        return grid;
    }

    @ResponseBody
    @RequestMapping(value = "/getTripCountData", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVo getTripCountData(HttpServletRequest request,
                                     @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                     @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime) {
        ResultVo result = new ResultVo();
        ServiceTrip tripQuery = new ServiceTrip();
        tripQuery.setStatus(TripStatus.Complete.getValue());
        tripQuery.setUserType(PlatformUserType.CLIENT_USER.getValue());
        AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            tripQuery.setCompanyId(admin.getCompanyId());
        }
        long st = 0;
        long et = 0;
        if (!StringUtils.isEmpty(startTime)) {
            st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            tripQuery.setStartCompleteTime(st);
        } else {
            String start = DateUtil.DatetoString(new Date(), "yyyy-MM-dd");
            st = SystemUtil.getAllSeconds(DateUtil.StringtoDate(start, "yyyy-MM-dd")) - 30 * 24 * 60 * 60;
            tripQuery.setStartCompleteTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            tripQuery.setEndCompleteTime(et);
        } else {
            et = SystemUtil.getAllSeconds();
            tripQuery.setEndCompleteTime(et);
        }
        List<ServiceTrip> triplist = serviceTripService.selectList("", tripQuery);
        if (triplist == null) return null;
        List<String> dateStr = DateUtil.getBetWeenDays(new Date(st * 1000), new Date(et * 1000), "yyyy-MM-dd");
        Map<String, Double> maps = new HashMap<>();
        for (ServiceTrip serviceTrip : triplist) {
            String tm = DateUtil.longToStr(serviceTrip.getCompleteTime() * 1000, "yyyy-MM-dd");
            if (maps.get(tm) == null) {
                maps.put(tm, serviceTrip.getPayFee());
            } else {
                double payFee = maps.get(tm);
                maps.put(tm, payFee + serviceTrip.getPayFee());
            }
        }
        List<TableBean> data = new ArrayList<>();
        for (String day : dateStr) {
            TableBean tableBean = new TableBean();
            tableBean.setName(day);
            Double payFre = maps.get(day);
            if (payFre != null && payFre > 0) {
                tableBean.setValue(payFre);
            }else {
                tableBean.setValue(0);
            }
            data.add(tableBean);
        }
        if (data == null || data.isEmpty()) {
            result.AddError("暂无统计数据");
        } else {
            result.AddOk(data);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/getTripNumData", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVo getTripNumData(HttpServletRequest request,
                                     @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                     @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime) {
        ResultVo result = new ResultVo();
        ServiceTrip tripQuery = new ServiceTrip();
        tripQuery.setStatus(TripStatus.Complete.getValue());
        tripQuery.setUserType(PlatformUserType.CLIENT_USER.getValue());
        AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            tripQuery.setCompanyId(admin.getCompanyId());
        }
        long st = 0;
        long et = 0;
        if (!StringUtils.isEmpty(startTime)) {
            st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            tripQuery.setStartCompleteTime(st);
        } else {
            String start = DateUtil.DatetoString(new Date(), "yyyy-MM-dd");
            st = SystemUtil.getAllSeconds(DateUtil.StringtoDate(start, "yyyy-MM-dd")) - 30 * 24 * 60 * 60;
            tripQuery.setStartCompleteTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            tripQuery.setEndCompleteTime(et);
        } else {
            et = SystemUtil.getAllSeconds();
            tripQuery.setEndCompleteTime(et);
        }
        List<ServiceTrip> triplist = serviceTripService.selectList("", tripQuery);
        if (triplist == null) return null;
        List<String> dateStr = DateUtil.getBetWeenDays(new Date(st * 1000), new Date(et * 1000), "yyyy-MM-dd");
        Map<String, Integer> maps = new HashMap<>();
        for (ServiceTrip serviceTrip : triplist) {
            String tm = DateUtil.longToStr(serviceTrip.getCompleteTime() * 1000, "yyyy-MM-dd");
            if (maps.get(tm) == null) {
                maps.put(tm, 1);
            } else {
                Integer num = maps.get(tm);
                maps.put(tm, num + 1);
            }
        }
        List<TableBean> data = new ArrayList<>();
        for (String day : dateStr) {
            TableBean tableBean = new TableBean();
            tableBean.setName(day);
            Integer num = maps.get(day);
            if (num != null && num > 0) {
                tableBean.setValue(num);
            }else {
                tableBean.setValue(0);
            }
            data.add(tableBean);
        }
        if (data == null || data.isEmpty()) {
            result.AddError("暂无统计数据");
        } else {
            result.AddOk(data);
        }
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/getUserNumData", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVo getUserNumData(HttpServletRequest request,
                                   @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                   @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime) {
        ResultVo result = new ResultVo();
        User tripQuery = new User();
        tripQuery.setEnabled(true);
        /*AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            tripQuery.setCompanyId(admin.getCompanyId());
        }*/
        long st = 0;
        long et = 0;
        if (!StringUtils.isEmpty(startTime)) {
            st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            tripQuery.setStartTime(st);
        } else {
            String start = DateUtil.DatetoString(new Date(), "yyyy-MM-dd");
            st = SystemUtil.getAllSeconds(DateUtil.StringtoDate(start, "yyyy-MM-dd")) - 30 * 24 * 60 * 60;
            tripQuery.setStartTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            tripQuery.setEndTime(et);
        } else {
            et = SystemUtil.getAllSeconds();
            tripQuery.setEndTime(et);
        }
        List<User> triplist = userService.selectList("", tripQuery);
        if (triplist == null) return null;
        List<String> dateStr = DateUtil.getBetWeenDays(new Date(st * 1000), new Date(et * 1000), "yyyy-MM-dd");
        Map<String, Integer> maps = new HashMap<>();
        for (User serviceTrip : triplist) {
            String tm = DateUtil.longToStr(serviceTrip.getRegisterTime() * 1000, "yyyy-MM-dd");
            if (maps.get(tm) == null) {
                maps.put(tm, 1);
            } else {
                Integer num = maps.get(tm);
                maps.put(tm, num + 1);
            }
        }
        List<TableBean> data = new ArrayList<>();
        for (String day : dateStr) {
            TableBean tableBean = new TableBean();
            tableBean.setName(day);
            Integer num = maps.get(day);
            if (num != null && num > 0) {
                tableBean.setValue(num);
            }else {
                tableBean.setValue(0);
            }
            data.add(tableBean);
        }
        if (data == null || data.isEmpty()) {
            result.AddError("暂无统计数据");
        } else {
            result.AddOk(data);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/getDriverNumData", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVo getDriverNumData(HttpServletRequest request,
                                   @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                   @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime) {
        ResultVo result = new ResultVo();
        Driver tripQuery = new Driver();
        tripQuery.setEnabled(true);
        AdminUser admin = (AdminUser) request.getSession().getAttribute(SESSION.USER);
        if (admin != null && admin.getRoleId() == CONFIG.COMPANY_ROLEID) {
            tripQuery.setCompanyId(admin.getCompanyId());
        }
        long st = 0;
        long et = 0;
        if (!StringUtils.isEmpty(startTime)) {
            st = SystemUtil.stringToSeconds(startTime, "yyyy-MM-dd");
            tripQuery.setStartTime(st);
        } else {
            String start = DateUtil.DatetoString(new Date(), "yyyy-MM-dd");
            st = SystemUtil.getAllSeconds(DateUtil.StringtoDate(start, "yyyy-MM-dd")) - 30 * 24 * 60 * 60;
            tripQuery.setStartTime(st);
        }
        if (!StringUtils.isEmpty(endTime)) {
            et = SystemUtil.stringToSeconds(endTime, "yyyy-MM-dd");
            et += 24 * 60 * 60;
            tripQuery.setEndTime(et);
        } else {
            et = SystemUtil.getAllSeconds();
            tripQuery.setEndTime(et);
        }
        List<Driver> triplist = driverService.selectList("", tripQuery);
        if (triplist == null) return null;
        List<String> dateStr = DateUtil.getBetWeenDays(new Date(st * 1000), new Date(et * 1000), "yyyy-MM-dd");
        Map<String, Integer> maps = new HashMap<>();
        for (Driver serviceTrip : triplist) {
            String tm = DateUtil.longToStr(serviceTrip.getOperateTime() * 1000, "yyyy-MM-dd");
            if (maps.get(tm) == null) {
                maps.put(tm, 1);
            } else {
                Integer num = maps.get(tm);
                maps.put(tm, num + 1);
            }
        }
        List<TableBean> data = new ArrayList<>();
        for (String day : dateStr) {
            TableBean tableBean = new TableBean();
            tableBean.setName(day);
            Integer num = maps.get(day);
            if (num != null && num > 0) {
                tableBean.setValue(num);
            }else {
                tableBean.setValue(0);
            }
            data.add(tableBean);
        }
        if (data == null || data.isEmpty()) {
            result.AddError("暂无统计数据");
        } else {
            result.AddOk(data);
        }
        return result;
    }

    private List<ServiceConfig> getServiceConfigList() {
        return serviceConfigService.selectAll("");
    }

    /**
     * 获取分公司列表
     *
     * @return
     */
    private List<Companypermit> getCompanyList() {
        Companypermit query = new Companypermit();
        query.setEnabled(true);

        return companypermitService.selectList("", query);
    }
}
